<?php

//@include 'dbmysqli.inc.php';
//@include 'queries.inc.php';
@include 'db.inc.php';

//echo getTypName(4);

//
////$ArchivSNrTest = 0;
//$SolutionNrTest = 12870001;
//$TypNrTest = 5;
//$StandortNrTest = 1;
//
//
//
//echo "<table id='ergebnis_table'>";
//    echo "<th width=100 align=left>Archiv Nr</th>";
//    echo "<th width=100 align=left>Archiv Text</th>";
//    echo "<th width=100 align=left>PL</th>";
//    echo "<th width=100 align=left>Standort</th>"; 
//    echo "<th width=100 align=left>Typ</th>";
//    echo "<th width=100 align=left>Status</th>";
//    echo "<th width=100 align=left>Solution Suchbegriff</th>";
//    echo "<th width=100 align=left>Solution Mandat</th>";
//    echo "<th width=100 align=left>Abacus-Projekt</th>";
//    echo "<th width=100 align=left>DatumInhalt von</th>";
//    echo "<th width=100 align=left>DatumInhalt bis</th>";
//    echo "<th width=100 align=left>Datum Archivierung</th>";
//    echo "<th width=100 align=left>Datum Vernichten</th>";
//    echo "<th width=100 align=left></th>";
//
//
//
//if ($stmt = $mysqli->prepare($QuerySelect."FK_SolutionNr= ? AND FK_TypNr = ?")){
//    $stmt->bind_param("ii", $SolutionNrTest, $TypNrTest);
//    $stmt->execute();
//    $stmt->store_result();
//    $zeilen = $stmt->num_rows;
//    echo $zeilen . " Einträge gefunden." . "<br>" . "<br>";
//    
//    $stmt->bind_result($iArchivNr, $iArchivText, $iProjektleiter, $iStandort, $iTypName, $iStatusName, 
//            $iSolutionSuchbegriff, $iSolutionMandat, $iAbacusName, $iDatumInhalt_von, $iDatumInhalt_bis, 
//            $iDatumArchivierung, $iDatumVernichten);
//  
//    while($stmt->fetch()){
//
//        echo "<tr>";       
//        echo "<td>" . $iArchivNr . "</td>";
//        echo "<td>" . $iArchivText . "</td>";
//        echo "<td>" . $iProjektleiter . "</td>";
//        echo "<td>" . $iStandort . "</td>";
//        echo "<td>" . $iTypName . "</td>";
//        echo "<td>" . $iStatusName . "</td>";
//        echo "<td>" . $iSolutionSuchbegriff . "</td>";
//        echo "<td>" . $iSolutionMandat . "</td>";
//        echo "<td>" . $iAbacusName . "</td>";
//        echo "<td>" . $iDatumInhalt_von . "</td>";
//        echo "<td>" . $iDatumInhalt_bis . "</td>";
//        echo "<td>" . $iDatumArchivierung . "</td>";
//        echo "<td>" . $iDatumVernichten . "</td>";
//        echo "<td><input type='submit' value='$iArchivNr' name='ArchivNr' ></td>";
//        echo "</tr>";     
//    }
//    $stmt->free_result();
//    $stmt->close();
//    
//    echo '</table>';
//    echo "</form>";
//    echo '</br></br>';   
//}


$select = "SELECT a.ArchivNr, a.ArchivText, a.Projektleiter, e.StandortNr, b.TypName, c.StatusName, d.SolutionSuchbegriff, d.SolutionMandat, f.AbacusName, a.DatumInhalt_von, a.DatumInhalt_bis, a.DatumArchivierung, a.DatumVernichten
FROM archiv_bracher.Archiv a
LEFT JOIN archiv_bracher.Typ b ON a.FK_TypNr=b.TypNr
LEFT JOIN archiv_bracher.Status c ON a.FK_StatusNr=c.StatusNr
LEFT JOIN archiv_bracher.Solution d ON a.FK_SolutionNr=d.SolutionNr
LEFT JOIN archiv_bracher.Standort e ON a.FK_StandortNr=e.StandortNr
LEFT JOIN archiv_bracher.Abacus f ON a.FK_AbacusNr=f.AbacusNr
WHERE FK_SolutionNr='12870001'";


mysql_query("SET NAMES 'latin_general_ci'");
$export = mysql_query($select);
$fields = mysql_num_fields($export);



for ($i = 0; $i < $fields; $i++) { 
    @$header .= mysql_field_name($export, $i) . "\t"; 
} 

while($row = mysql_fetch_row(@$export)) { 
    @$line = ''; 
    foreach($row as $value) {                                             
        if ((!isset($value)) OR ($value == "")) { 
            $value = "\t"; 
        } else { 
            $value = str_replace('"', '""', $value); 
            $value = '"' . $value . '"' . "\t"; 
        } 
        $line .= $value; 
    } 
    @$data .= trim($line)."\n"; 
} 
@$data = str_replace("\r","",$data);  

if ($data == "") { 
    $data = "\n(0) Records Found!\n";                         
} 

$excel_export_filename = "archiv_export_".date("Y-m-d_H-i",time());

header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=".$excel_export_filename.".xls"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 
@print "$header\n$data";  







?>
